<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class Roles extends Dispatcher {
  private $rid;
  private $name;
  private $msg;

  function setRid ($rid) { $this->rid = $rid; }
  function setName ($name) { $this->name = $name; }
  function setMsg ($msg) {$this->msg = $msg; }

  function getRid() { return $this->rid; }
  function getName() { return $this->name; }
  function getMsg() { return $this->msg; }

  function getRoles() {
    $rs = $this->getDb()->GetAll("SELECT * FROM core_roles ORDER BY FIELD(rid,3,2,1) DESC, name ");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return $rs; }
  }

  function getRoleName() {
    $rs = $this->getDb()->GetOne("SELECT name FROM core_roles WHERE rid='".$this->getRid()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return $rs; }
  }

  function checkExists($op) {
    if ($op == 'modify') {
      $rs = $this->getDb()->GetRow("SELECT * FROM core_roles WHERE name='".$this->getName()."' AND rid !='".$this->getRid()."'");
      if(!$rs) { return true; } else { return false; }
    } else {
      $rs = $this->getDb()->GetRow("SELECT * FROM core_roles WHERE name='".$this->getName()."'");
      if(!$rs) { return true; } else { return false; }
    }
  }

  function addRole() {
    $rs = $this->getDb()->Execute("INSERT INTO core_roles set name='".$this->getName()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); return false; } else { return true; }
  }

  function modifyRole() {
    $rs = $this->getDb()->Execute("UPDATE core_roles SET name='".$this->getName()."' WHERE rid='".$this->getRid()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return true; }
  }

  function deleteRole() {
 #   $rs = $this->getDb()->Execute("DELETE roles, permissions FROM roles, permissions WHERE roles.rid='".$this->getRid()."'");
    $rs = $this->getDb()->Execute("DELETE FROM core_roles WHERE rid='".$this->getRid()."'");
    $rs = $this->getDb()->Execute("DELETE FROM core_permissions WHERE rid='".$this->getRid()."'");
    $rs = $this->getDb()->Execute("DELETE FROM core_users_roles WHERE rid='".$this->getRid()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return true; }
  }

} //end of class

?>